package hku.cs.lunuo.cfg;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class XComparor {
	static SAXBuilder sbuilder = new SAXBuilder();
	
	
	
	public static void main(String []args) {
		File filea = new File(args[0]);
		File fileb = new File(args[1]);
		new XComparor().compareBlk(filea, fileb);
		
	}
	
	public void compareStmt(File filea, File fileb) {
		
		
		List<EntityStmt> alist = new ArrayList<EntityStmt>();
		List<EntityStmt> blist = new ArrayList<EntityStmt>();
		
	
			try {
				readStmtFile(alist, filea);
				readStmtFile(blist, fileb);
			} catch (JDOMException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		
		
//		blist.retainAll(alist);
//		System.out.println(blist.size());
		
		
		Collections.sort(alist);

		for(int i=0; i<alist.size(); i++) {
			if(!blist.contains(alist.get(i))){
				System.out.println(alist.get(i).klassName+" "+alist.get(i).i);
			}
		}
	}
	
	
public void compareBlk(File filea, File fileb)  {
		
		
		List<EntityBlk> alist = new ArrayList<EntityBlk>();
		List<EntityBlk> blist = new ArrayList<EntityBlk>();
		
		
			try {
				readBlkFile(alist, filea);
				readBlkFile(blist, fileb);
			} catch (JDOMException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		
		
//		blist.retainAll(alist);
//		System.out.println(blist.size());
		
		
		Collections.sort(alist);

		for(int i=0; i<alist.size(); i++) {
			if(!blist.contains(alist.get(i))){
				System.out.println(alist.get(i).klassName+" "+alist.get(i).methodName+" "+alist.get(i).idx);
			}
		}
	}
	
	public void readStmtFile(List<EntityStmt> ls, File file) throws JDOMException, IOException {
		Document xmlDoc = sbuilder.build(file);
		Element xmlRoot = xmlDoc.getRootElement();
		Iterator xmlIt = xmlRoot.getChildren().iterator();
		while(xmlIt.hasNext()) {
			Element xmlStmt = (Element)xmlIt.next();
			
			String klassName = xmlStmt.getAttributeValue("class");
			int index = Integer.parseInt(xmlStmt.getAttributeValue("line"));
			
			EntityStmt entity = new EntityStmt(klassName, index);
			
			ls.add(entity);
		}
	}
	
	public void readBlkFile(List<EntityBlk> ls , File file) throws JDOMException, IOException {
		Document xmlDoc = sbuilder.build(file);
		Element xmlRoot = xmlDoc.getRootElement();
		Iterator xmlIt = xmlRoot.getChildren().iterator();
		while(xmlIt.hasNext()) {
			Element xmlBlk = (Element)xmlIt.next();
			
			String klassName = xmlBlk.getAttributeValue("class");
			String methodName = xmlBlk.getAttributeValue("method");
			int idx = Integer.parseInt(xmlBlk.getAttributeValue("idx"));
			
			EntityBlk entity = new EntityBlk(klassName, methodName, idx);
			ls.add(entity);
		}
	}

}
